#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef short int bool;
#define true    1
#define false   0

#define M 52
#define N 20
int count[M] = {0};

char word[N];
int  word_len = 0;

int getindexbych(char ch)
{
    if(ch >= 'A' && ch <= 'Z')
    {
        return (ch-'A')*2;
    }
    else
    {
        return (ch-'a')*2+1;
    }
}

char getchbyindex(int i)
{
    if(i % 2 == 0)
    {
        i /= 2;
        return i+'A';
    }
    else
    {
        i -= 1;
        return i/2 + 'a';
    }
}

void statistic()
{
    int i = 0, index = -1;

    word_len = strlen(word);

    memset(&count, 0, M*4);

    for(i = 0; i < word_len; i++)
    {
        index = getindexbych(word[i]);
        count[index]++;
    }
//    for(i = 0; i < M; i++)
//    {
//        printf("%c:%d\t", getchbyindex(i), count[i]);
//    }
//    printf("\n");
}

void fn(char newword[N], int k)
{
    char ch;
    int index = -1;
    if(k == word_len)
    {
        newword[k] = '\0';
        printf("%s\n", newword);
    }
    else
    {
        for(index = 0; index < M; index++)
        {
            if(count[index] == 0)
            {
                continue;
            }
            ch = getchbyindex(index);
            count[index]--;
            newword[k] = ch;
            fn(newword, k+1);
            count[index]++;
        }
    }
}

void process()
{
    char newword[N];
    statistic();

    fn(newword, 0);
}

int main()
{
    //freopen("bin\\debug\\in.txt", "r", stdin);
    int i = 0, n = 0;

    scanf("%d", &n);
    for(i = 0; i < n; i++)
    {
        scanf("%s", word);
        process();
    }

    return 0;
}
